//
//  MainViewCell.m
//  Emoticon
//
//  Created by ytkjs2 on 2020/3/30.
//  Copyright © 2020 ytk. All rights reserved.
//

#import "MainViewCell.h"
#import "FLAnimatedImage.h"

@interface MainViewCell ()
{
    NSMutableArray * btnArray;
}

@end


@implementation MainViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    btnArray = [NSMutableArray array];
    
    CGFloat width = (rScreenWidth - 70)/4;
    
    for (int i = 0; i < 8; i ++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(10 + (width + 10) * (i%4), 10 + (width + 10) * (i/4), width, width)];
//        btn.backgroundColor = [UIColor blackColor];
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:4.5];
        [btnArray addObject:btn];
        [self.contentView addSubview:btn];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setBtnAction:(NSArray *)arr withTag:(int)tag
{
    for (int i = 0; i < btnArray.count; i ++) {
        UIButton * btn = btnArray[i];
        FLAnimatedImageView *gifImgView = [[FLAnimatedImageView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height)];
        PicModel * model = arr[i];
        NSString * imgUrl = [model.image stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        uint8_t c;
        NSData * imgData = [self imageDataFromDiskCacheWithKey:imgUrl];
        [imgData getBytes:&c length:1];
        //判断动图
        if (c == 0x47) {
            
            NSURL *url = [NSURL URLWithString:imgUrl];
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                
                [[[SDWebImageManager sharedManager] imageCache] storeImage:image
                                                                 imageData:nil
                                                                    forKey:url.absoluteString
                                                                    toDisk:NO
                                                                completion:nil];
                
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    
                    if (data != nil) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self animatedImageView:gifImgView
                                               data:data];
//                            [btn setBackgroundImage:gifImgView.image forState:0];
                            [btn addSubview:gifImgView];
                        });
                    }
                });
                
            }];
        }
        else
            [btn sd_setImageWithURL:[NSURL URLWithString:imgUrl] forState:0 placeholderImage:[UIImage imageNamed:@"icon_placeholderImage"]];
   
        btn.tag = [[NSString stringWithFormat:@"%d0%d",tag,i] intValue];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)btnAction:(UIButton *)btn
{
    [self.controller performSelector:@selector(btnAction:) withObject:btn];
}

- (void)animatedImageView:(FLAnimatedImageView *)imageView data:(NSData *)data {
    
    FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:data];
    imageView.animatedImage   = gifImage;
}

- (NSData *)imageDataFromDiskCacheWithKey:(NSString *)key {
    
    NSString *path = [[[SDWebImageManager sharedManager] imageCache] defaultCachePathForKey:key];
    return [NSData dataWithContentsOfFile:path];
}

@end
